<?php

App::uses('AppModel', 'Model');
App::uses('CompositeKeyModel', 'Model');

/**
 * OrganisationMemberGroup Model
 *
 * @property Member $Member
 * @property OrganisationGroup $OrganisationGroup
 */
class OrganisationMember extends CompositeKeyModel {

    //The Associations below have been created with all possible keys, those that are not needed can be removed

    public $useDbConfig = "organisations";
    public $primaryKeys = array('id', 'organisation_id');

    /**
     * belongsTo associations
     *
     * @var array
     */
    public $belongsTo = array(
        'Organisation' => array(
            'className' => 'Organisation',
            'foreignKey' => 'organisation_id',
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'counterCache' => true,
            'counterScope' => array(
                'OrganisationMember.active' => 1,
                'OrganisationMember.approved' => 1
            )
        ),
        'OrganisationMemberCategory' => array(
            'className' => 'OrganisationMemberCategory',
            'foreignKey' => 'organisation_member_category_id',
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'counterCache' => true,
            'counterScope' => array(
                'OrganisationMember.active' => 1,
                'OrganisationMember.approved' => 1
            )
        ),
        'Member' => array(
            'className' => 'Member',
            'foreignKey' => 'member_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Approver' => array(
            'className' => 'OrganisationAccount',
            'foreignKey' => 'approved_by',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'MemberImage' => array(
            'className' => 'MemberImage',
            'foreignKey' => 'member_image_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );
    
    public $hasOne = array(
        'OrganisationMemberNote' => array(
            'className' => 'OrganisationMemberNote',
            'foreignKey' => 'organisation_member_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'OrganisationAccount' => array(
            'className' => 'OrganisationAccount',
            'foreignKey' => 'organisation_member_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'OrganisationMemberImage' => array(
            'className' => 'OrganisationMemberImage',
            'foreignKey' => 'organisation_member_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );
    
    public $hasMany = array(
        'OrganisationMemberContribution' => array(
            'className' => 'OrganisationMemberContribution',
            'foreignKey' => 'organisation_member_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'OrganisationMemberAnniversary' => array(
            'className' => 'OrganisationMemberAnniversary',
            'foreignKey' => 'organisation_member_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'OrganisationMemberGroup' => array(
            'className' => 'OrganisationMemberGroup',
            'foreignKey' => 'organisation_member_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'OrganisationAccount' => array(
            'className' => 'OrganisationAccount',
            'foreignKey' => 'organisation_member_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );
    
    public function getAssociation($organisation_id, $member_id) {
        return $this->find('first', array(
            'conditions' => array(
                'OrganisationMember.organisation_id' => $organisation_id,
                'OrganisationMember.member_id' => $member_id
            )
        ));
    }
    
    public function getTotalMembers($organisation_id) {
        return $this->find('count', array(
            'conditions' => array(
                'OrganisationMember.organisation_id' => $organisation_id,
                'OrganisationMember.active' => 1
            )
        ));
    }
    
    public function getTotalMembersByGender($organisation_id, $gender) {
        return $this->find('count', array(
            'conditions' => array(
                'OrganisationMember.organisation_id' => $organisation_id,
                'Member.gender' => $gender,
                'Member.active' => 1
            )
        ));
    }
    
    public function getMembersForList($organisation_id, $offset = 0, $limit = 40, $sort_order = 'recent') {
        $result = $this->find('all', array(
            'fields' => array('Member.id', 'Member.first_name', 'Member.last_name', 'Member.gender', 'Member.email', 'Member.dob', 'Member.mobile_number', 'organisation_id', 'organisation_no'),
            'conditions' => array(
                'OrganisationMember.organisation_id' => $organisation_id,
                'OrganisationMember.active' => 1,
                'Member.status' => 'active',
                'Member.active' => 1
            ),
            'order' => $sort_order == 'alpha' ? array('Member.last_name' => 'asc') : array('OrganisationMember.id' => 'desc'), 
            'limit' => $limit,
            'offset' => $offset
        ));
        
        return $result;
    }


}
